<?php
//$Id: faqs.php 9 2009-04-26 06:35:12Z bole.Chen $
uses('controller');

class faqsController extends Controller {

    function __construct() {
        parent::__construct();
    }

    function faqIndex() {
        $this->debugmsg('faqsController::faqIndex');

        //单个
        if ($this->request['id']) $result = $this->faqList();

        if (!$result['list']) {
            unset($this->request['id']);
            $data['pagesize']   = 50;
            $data['sort']       = 'views';
            $data['sortorder']  = 'DESC';

            $result['hot'] = $this->faqList($data);
        }

        return $result;
    }

    function faqList($v=null) {
        $this->debugmsg('faqsController::faqList');

        $data               = $v;
        $data['id']         = intval($this->request['id']);
        $data['sort']       = ($this->request['sort'] ? strtolower($this->request['sort']) : $v['sort']);
        $data['sortorder']  = ($this->request['sortorder'] ? strtoupper($this->request['sortorder']) : $v['sortorder']);

        if ($this->clearSearchStr($this->request['s']))    {
            $data['search']     = $this->clearSearchStr($this->request['s']);
            $data['sfields']    = array('q', 'a'); //搜索问题和答案
        }

        if ($this->_var['where'])   $data['where']  = $this->_var['where'];

        $result = $this->model->faqList($data);

        $result['data'] = $data;

        return $result;
    }

    function adminList() {
        $this->debugmsg('faqsController::adminList');

        $result['list'] = $this->faqList();

        return $result;
    }

    function adminEdit() {
        $this->debugmsg('faqsController::adminEdit');

        $data['id']     = $this->request['id'];
        $data['remove'] = $this->request['remove'];
        $data['add']    = $this->request['add'];

        $result['data'] = $data;

        if ($this->post['q'] && $this->post['a'] || $data['remove']) {

            $data['fields']['q']        = trim($this->post['q']);
            $data['fields']['a']        = trim($this->post['a']);
            $data['fields']['views']    = intval($this->post['views']);
            $data['fields']['updated']  = time();

            $result = $this->model->faqsEdit($data);
            $result['ajaxcode'] = 1;

            if ($result['remove'] || $result['add']) $this->redirect('faqs.php');
        } else {
            $result['ajaxcode'] = 2;
        }

        if (!$data['add']) {
            $tmp = $this->model->faqList($data);
            $result['data'] = &$tmp['list'];
        }

        return $result;
    }

    function ajaxAdminEdit($var) {
        parse_str($var, $this->post);
        $this->post = array_stripslashes($this->post);
        $result = $this->adminEdit();
        $this->ajax->quickresult($result['ajaxcode']);
    }

}
?>
